// pages/bookmanager/bookmanager.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    placeHolder: '输入你要查找的内容',
    autoFocus: false,
    inputEnable: true,
    keydown_number: 0,
    input_value: "",
    bookmsg: true,
  },
  skip: 0,

  redictAct: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../bookmanager2/bookmanager2?id=' + id
    });
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 70) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },

  getData: function (skip) {
    const db = wx.cloud.database()
    db.collection("book_list")
      .count().then(res => {
        this.setData({
          total: res.total,
        })
          const db = wx.cloud.database()
          db.collection("book_list")
        
            .skip(skip)
            .get({
              success: res => {
                this.skip += 20
                this.data.list = this.data.list.concat(res.data)
                this.setData({
                  list: this.data.list,
                })
              },
              fail: err => {
                console.log(err)
              }
            })
      })
  },
  
  onReachBottom: function () {
    wx.showLoading({
      title: '正在加载',
      duration: 1000
    })
    this.getData(this.skip)
  },
  onInput: function (e) {
    this.setData({
      searchKey: e.detail.value,
    })
    if (e.detail.cursor != 0) {
      this.setData({
        keydown_number: 1
      })
    } else {
      this.setData({
        keydown_number: 0
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
  },
  onShow: function () {
    this.setData({
      list :[]
    })
    this.getData(this.skip=0)
  },

  formSubmit: function (e) {
    if (this.data.keydown_number == 1) {
      this.setData({
        key: this.data.searchKey
      })
      const db = wx.cloud.database();
      const _ = db.command
      db.collection("book_list")
        .where(_.or([{
            title: db.RegExp({
              regexp: '.*' + this.data.key,
              options: 'i',
            })
          },
          {
            author: db.RegExp({
              regexp: '.*' + this.data.key,
              options: 'i',
            })
          },
          {
            borrow: db.RegExp({
              regexp: '.*' + this.data.key,
              options: 'i',
            })
          }
        ]))
        .get({
          success: res => {
            this.setData({
              list: res.data,
              booksum: res.data.length,
            })
            console.log(res.data.length)
            if (this.data.list.length == 0) {
              wx.showToast({
                icon: 'none',
                title: '没找到相关书籍',
                duration: 2000
              })
              this.setData({
                bookmsg: false,
              })
            } else {
              this.setData({
                bookmsg: true,
              })
            }
          },
          fail: err => {
            console.log(err)
          }
        })
    } else {
      wx.showModal({
        title: '提示',
        content: '请输入内容',
        showCancel: false,
      });
    }
  },
  onClear: function () {
    this.setData({
      searchKey: '',
      bookmsg: false
    })

  },
})